package com.team2.cyber_eye;

import java.util.ArrayList;

import com.team2.cyber_eye.ActivityHelpers.Util;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class Options extends CyberActivity 
{
	public static final String MAX_SEARCH_DIST = "Max search distance";
	public static final float MAX_SEARCH_DIST_DEFAULT = 20;
	public static final String SORT_PRODUCT = "Sort Product By...";
	public static final String SORT_PRODUCT_DEFAULT = "Price   Low-High";
	public static final String SEARCH_TIMEOUT = "Search Timeout";
	public static final int SEARCH_TIMEOUT_DEFAULT = 3;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Bundle extras = this.getIntent().getExtras();
		
		if (extras != null && extras.getBoolean("Init"))
		{
			try
			{
				super.onCreate(savedInstanceState, R.layout.activity_options, "Home");
	
				Options.Initialize(this);
	
				Intent intent = new Intent(this, Home.class);
				startActivity(intent);
				this.finish();
				return;
				}
			catch (Throwable ex)
			{
				ex.toString();
			}
		}
		
		super.onCreate(savedInstanceState, R.layout.activity_options, "OPTIONS");

		Spinner spinner = (Spinner) findViewById(R.id.sortingSpinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.sorting_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		
		loadOptions();
		
		Button saveButton = (Button)this.findViewById(R.id.SaveButton);
		saveButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				saveOptions();
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.global_menu, menu);
		return true;
	}

	@SuppressWarnings("deprecation")
	void loadOptions()
	{
		SharedPreferences settings;
		
		settings = this.getPreferences(MODE_WORLD_WRITEABLE);
		float MaxSearch = settings.getFloat(MAX_SEARCH_DIST, MAX_SEARCH_DIST_DEFAULT);
		String SortPref = settings.getString(SORT_PRODUCT, SORT_PRODUCT_DEFAULT);
		int searchTimeout = settings.getInt(SEARCH_TIMEOUT, SEARCH_TIMEOUT_DEFAULT);
		
		TextView MaxSearchBox = (TextView)this.findViewById(R.id.SearchRadius);
		MaxSearchBox.setText(""+MaxSearch);
		
		Spinner spinner = (Spinner) findViewById(R.id.sortingSpinner);
		Util.SetSpinnerTo(spinner, SortPref);
		
		TextView SearchTimeoutBox = (TextView)this.findViewById(R.id.SearchTimeout);
		SearchTimeoutBox.setText(""+searchTimeout);
	}

	@SuppressWarnings("deprecation")
	void saveOptions()
	{
		SharedPreferences settings;
		SharedPreferences.Editor editor;
		
		settings = this.getPreferences(MODE_WORLD_WRITEABLE);
		
		EditText MaxSearchBox = (EditText)this.findViewById(R.id.SearchRadius);
		float MaxSearch = Float.NaN;
		
		try { 
			MaxSearch = Float.parseFloat(MaxSearchBox.getText().toString()); 
		} catch (Exception ex) 
		{}//can't happen since edit box is set to numeric and only allows numbers
		
		Spinner spinner = (Spinner) findViewById(R.id.sortingSpinner);
		String SortType = spinner.getSelectedItem().toString();
		
		EditText SearchTimeoutBox = (EditText)this.findViewById(R.id.SearchTimeout);
		int timeout = 0;
		
		try{
			timeout = Integer.parseInt(SearchTimeoutBox.getText().toString());
		}catch(Exception ex){}
		
		
		//update static values
		SearchDistance = MaxSearch;
		SortByType = SortType;
		searchTimeout = timeout;
		
		editor = settings.edit();
		editor.putFloat(MAX_SEARCH_DIST, MaxSearch);
		editor.putString(SORT_PRODUCT, SortType);
		editor.putInt(SEARCH_TIMEOUT, timeout);
		editor.commit();
		
		this.finish();
	}
	
	
	static Activity Source = null;
	static float SearchDistance;
	static String SortByType;
	static int searchTimeout;

	@SuppressWarnings("deprecation")
	public static void Initialize(Activity splash)
	{
		SharedPreferences settings;
		
		settings = splash.getPreferences(MODE_WORLD_WRITEABLE);
		SearchDistance = settings.getFloat(MAX_SEARCH_DIST, MAX_SEARCH_DIST_DEFAULT);
		SortByType = settings.getString(SORT_PRODUCT, SORT_PRODUCT_DEFAULT);
		searchTimeout = settings.getInt(SEARCH_TIMEOUT, SEARCH_TIMEOUT_DEFAULT);
		
	}
	
	public static double GetSearchDistance()
	{
		return SearchDistance;
	}

	public static String GetProductSort()
	{
		return SortByType;
	}

	public static int getSearchTimeout() {
		return searchTimeout;
	}
}
